package com.atstudy.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2023/1/4 10:48
 * @copyright: 博为峰
 */
public class StreamDemo {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("张三", 20, 96));
        list.add(new Student("李四", 30, 86));
        list.add(new Student("王五", 50, 85));
        list.add(new Student("赵六", 10, 90));
        list.add(new Student("张三", 20, 96));
        list.add(new Student("德刚", 46, 70));
        list.add(new Student("于谦", 50, 82));
        list.add(new Student("云鹏", 35, 66));
        list.add(new Student("孙越", 29, 99));
        list.add(new Student("王二", 30, 76));

        //获取Stream接口实现类对象
        Stream<Student> stream = list.stream();

//        m1(stream);
//        m2(stream);
//        m3(stream);
//        m4(stream);
//        m5(stream);

    }

    private static void m5(Stream<Student> stream) {
        List<String> toList = stream.map(new Function<Student, String>() {
            @Override
            public String apply(Student student) {
                return student.getName();
            }
        }).collect(Collectors.toList());

        System.out.println(toList);
    }

    private static void m4(Stream<Student> stream) {
        //分页
//        stream.limit(3).forEach(System.out::println);
        stream.skip(3).limit(3).forEach(System.out::println);
    }

    private static void m3(Stream<Student> stream) {
        //去重，总数
        System.out.println(stream.distinct().count());
    }

    private static void m2(Stream<Student> stream) {
        //过滤
        stream.filter(new Predicate<Student>() {
            @Override
            public boolean test(Student student) {
                return student.getName().startsWith("王");
            }
        }).forEach(System.out::println);

        stream.filter(student -> student.getName().startsWith("王")).forEach(System.out::println);
    }

    private static void m1(Stream<Student> stream) {
        //遍历
        stream.forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
//                System.out.println(student);
            }
        });

        stream.forEach(student -> System.out.println(student));

        stream.forEach(System.out::println);
    }
}
